package com.wyh.lotterysystem.common.intercepter;

import com.wyh.lotterysystem.common.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求头中的token
        String token = request.getHeader("user_token");
        log.info("获取token：{}",token);

        //使用JwtUtil中的方法解析传过来的参数，如果返回null就代表解密失败
        Claims b = JWTUtil.decode(token);

        if (b != null){
            log.info("解析成功");
            return true;
        }else {
            log.error("解析失败");
            response.setStatus(401);
            return false;
        }
    }
}
